﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace BlueLotus7.SSQAnalysisCore
{
    public static class Extensions
    {
        #region StringBuilder

        /// <summary>
        /// Extend AppendLineWithFormat for <c>StringBuilder</c>.
        /// </summary>
        /// <param name="stringBuilder"></param>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public static void AppendLineWithFormat(this StringBuilder stringBuilder, string format, params object[] args)
        {
            stringBuilder.AppendLine(string.Format(format, args));
        }

        #endregion

        #region Percentaging

        /// <summary>
        /// Extend Percentaging for <c>Decimal</c>.
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public static string Percentaging(this Decimal instance)
        {
            return instance.Percentaging("00.0000%");
        }

        /// <summary>
        /// Extend Percentaging for <c>Decimal</c>.
        /// </summary>
        /// <param name="instance"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string Percentaging(this Decimal instance, string format)
        {
            return (instance).ToString(format);
        }

        #endregion

        #region Xml

        /// <summary>
        /// Extend method for getting xml attribute value by node instance and key string.
        /// </summary>
        /// <param name="node">Node instance.</param>
        /// <param name="key">Key string for identifying the attribute.</param>
        /// <returns>Value in string type.</returns>
        public static string GetAttributeValue(this XmlNode node, string key)
        {
            return (node != null && !string.IsNullOrEmpty(key) && node.Attributes[key] != null)
                ? (node.Attributes[key].Value)
                : string.Empty;
        }

        /// <summary>
        /// Extend method for getting xml attribute value by node instance and key string.
        /// </summary>
        /// <param name="node">Node instance.</param>
        /// <param name="key">Key string for identifying the attribute.</param>
        /// <returns>Value in string type.</returns>
        public static string GetAttributeValue(this XElement node, string key)
        {
            return (node != null && !string.IsNullOrEmpty(key) && node.Attribute(key) != null)
                ? (node.Attribute(key).Value)
                : string.Empty;
        }

        #endregion

        #region List<SSQLotteryStage>

        public static int SortByIdentity(SSQLotteryStage stage1, SSQLotteryStage stage2)
        {
            return stage1.LotteryIdentity.CompareTo(stage2.LotteryIdentity);
        }

        public static void SortByIdentity(this List<SSQLotteryStage> stages)
        {
            stages.Sort(SortByIdentity);
        }

        #endregion

        #region Sort for KeyValuePair<int, decimal>

        /// <summary>
        /// Implement IComparison interface for <c>KeyValuePair&lt;int, decimal&gt;</c>
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        public static int SortPercentageDESC(KeyValuePair<int, decimal> item1, KeyValuePair<int, decimal> item2)
        {
            return -1 * item1.Value.CompareTo(item2.Value);
        }

        /// <summary>
        /// Extend SortPercentageDESC for <c>List&lt;KeyValuePair&lt;int, decimal&gt;&gt;</c>.
        /// </summary>
        /// <param name="items"></param>
        public static void SortPercentageDESC(this List<KeyValuePair<int, decimal>> items)
        {
            if (items != null)
            {
                items.Sort(SortPercentageDESC);
            }
        }

        #endregion
    }
}
